package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderRole;
import com.itheima.service.OrderRoleService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpSession;

/*
 *@AUTHOR:LIUCHAO;
 *@DATE: 2020/9/13 8:58
 */
@RestController
@RequestMapping("/orderInfo")
public class OrderInfoController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderRoleService orderRoleService;
@RequestMapping("/order")
    public Result order(@RequestBody OrderRole orderRole){
    System.out.println(orderRole);

    String codeInRedis = jedisPool.getResource().get(orderRole.getTelephone()+RedisMessageConstant.SENDTYPE_ORDER);
    String telephone =jedisPool.getResource().get("order"+orderRole.getTelephone());

    if (!(codeInRedis.equals(orderRole.getValidateCode())&&telephone.equals(orderRole.getTelephone()))){
        return new Result(false, MessageConstant.VALIDATECODE_ERROR);
    }
    Result result=null;
       result=  orderRoleService.add(orderRole);
       if (result.isFlag()) {
           try {
               SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, orderRole.getOrderDate().toString());
           } catch (ClientException e) {
               e.printStackTrace();
           }
       }
    return result;
}


    @RequestMapping("/orderSuccess")
    public Result orderSuccess(Integer id){

    return orderRoleService.orderSuccess(id);
    }

}
